<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form>
        <input type="text" name="username">
        
        <input type="submit" value="提交">
    </form>
</body>

<script>
    var btn = document.querySelector('[type="submit"]');

    btn.onclick = function(event) {
        alert('你点击了提交按钮');

        var text = document.querySelector('[type="text"]');

        event.preventDefault();

        if (text.value) {
            alert('提交成功');
        } else {
            alert('未输入，提交失败');
        }
    }
</script>
</html>